package com.example.myretrofit2.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myretrofit2.R;
import com.example.myretrofit2.presenter.MyPresenter;
import com.example.myretrofit2.presenter.MySubscriber;

public class UpdateActivity extends AppCompatActivity {
    EditText oldpass;
    EditText newpass;
    EditText checkpass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        oldpass = findViewById(R.id.oldpass_up);
        newpass = findViewById(R.id.newpass_up);
        checkpass = findViewById(R.id.checkPass_up);
    }
    public void savePass(View view){
        String oldpassword = oldpass.getText().toString();
        String newpassword = newpass.getText().toString();
        String checkpassword = checkpass.getText().toString();
        if(TextUtils.isEmpty(oldpassword)){
            Toast.makeText(this,"请输入原始密码",Toast.LENGTH_SHORT).show();
        }else {
            SharedPreferences preferences = this.getSharedPreferences("user",MODE_PRIVATE);
            String password = preferences.getString("userPass","");
            if(!TextUtils.isEmpty(password)){
                if(password.equals(oldpassword)){
                    if(TextUtils.isEmpty(newpassword)||TextUtils.isEmpty(checkpassword)){
                        Toast.makeText(this,"请输入新密码",Toast.LENGTH_SHORT).show();
                    }else if(!newpassword.equals(checkpassword)){
                        Toast.makeText(this,"两次密码不相等",Toast.LENGTH_SHORT).show();
                    }else {
                        MyPresenter.updateUser(newpassword, new MySubscriber<Boolean>(this) {
                            @Override
                            public void onNext(Boolean b) {
                                if(b){
                                    Intent intent = new Intent(UpdateActivity.this,LoginActivity.class);
                                    startActivity(intent);
                                }

                            }
                        });
                    }
                }else{
                    Toast.makeText(this,"密码不正确",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}